package com.woniuxy.controllers;

import com.woniuxy.entitys.Company;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.services.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "公司（客户）管理接口")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @ApiOperation(
            value = "查询所有公司",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //获取所有客户信息
    @GetMapping("/companies")
    public RestPOJO getAllCompanies(){
        RestPOJO restPOJO = new RestPOJO();
        List<Company> companyList = companyService.getAllCompanies();
        System.out.println(companyList);
        if(companyList != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(companyList);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "获取所有已流失客户信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //获取所有已流失客户信息
    @GetMapping("/lostcompanies")
    public RestPOJO getAllLostCompanies(){
        RestPOJO restPOJO = new RestPOJO();
        List<Company> companyList = companyService.getAllLostCompanies();
        System.out.println(companyList);
        if(companyList != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(companyList);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据公司id查询公司信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="com_id",value="公司id",required=true),
    })

    @GetMapping("/companies/{com_id}")
    public RestPOJO selectByPrimaryKey(@PathVariable("com_id") Integer com_id){
        RestPOJO restPOJO = new RestPOJO();
        Company company = companyService.selectByPrimaryKey(com_id);
        if(company != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(company);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "更新公司信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存修改的客户信息
    @PostMapping("/companies")
    public RestPOJO saveUpdCom(@RequestBody Company company){
        RestPOJO restPOJO = new RestPOJO();
        Integer result = companyService.updateByPrimaryKey(company);
        if(result > 0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据名字（cli_name）和公司id查询联系人信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存新增的客户信息
    @PostMapping("/companies/add")
    public RestPOJO saveAddCom(@RequestBody Company company){
        //设置客户状态（com_status）为1，1代表正常交往
        company.setCom_status(1);
        RestPOJO restPOJO = new RestPOJO();
        Integer result = companyService.insertSelective(company);
        //获取新增公司主键iD,从而去添加联系人@liyang
        Integer com_id=company.getCom_id();
        if(result > 0){
            //存入rest,传到客户端@liyang
            restPOJO.setData(com_id);
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "删除用户（用户状态com_status改为0）",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="com_id",value="公司id",required=true)
    })
    //删除用户（用户状态com_status改为0）
    @PostMapping("/companies/del/{com_id}")
    public RestPOJO updStatus(@PathVariable("com_id") String com_id){
        RestPOJO restPOJO = new RestPOJO();
        //状态为0表示不与此公司进行交往，即为删除
        String com_status = "0";
        Integer result = companyService.updStatus(com_status,com_id);
        if(result > 0){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据工资名字查询公司信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="com_name",value="公司名字",required=true)
    })
    //模糊查询(根据公司名字)
    @PostMapping("/companies/search/{com_name}")
    public RestPOJO search(@PathVariable("com_name") String com_name){
        RestPOJO restPOJO = new RestPOJO();
        List<Company> companyList = new ArrayList<>();
        if("".equals(com_name)){
            companyList = companyService.getAllCompanies();
        }else{
            com_name = "%" + com_name + "%";
            companyList = companyService.search(com_name);
        }
        if(companyList != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(200);
            restPOJO.setData(companyList);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(250);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据满意度分组查询公司数量",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //根据满意度分组查询公司数量
    @GetMapping("/satisfy/count")
    public RestPOJO getCountBySatisfy(){
        RestPOJO restPOJO = new RestPOJO();
        List<Map<String, Integer>> countBySatisfy = companyService.getCountBySatisfy();
        if(countBySatisfy != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(countBySatisfy);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }

        return restPOJO;
    }

    @ApiOperation(
            value = "根据信用度分组查询公司数量",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //根据信用度分组查询公司数量
    @GetMapping("/credit/count")
    public RestPOJO getCountByCredit(){
        RestPOJO restPOJO = new RestPOJO();
        List<Map<String, Integer>> countByCredit = companyService.getCountByCredit();
        if(countByCredit != null){
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(countByCredit);
        }else {
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }

        return restPOJO;
    }


}
